package com.lzx.iteam.net;

import android.content.Context;
import android.util.Log;
import com.lzx.iteam.bean.SecondCallData;
import com.lzx.iteam.util.Constants;

import java.io.*;

public class StorageData {
    
    private static final String TAG = "StorageData";
    
    private static final String CHEAP_CALL_FILE = "cheapcall.data";
    
    private Serializable mSerializeData;
    private ObjectOutputStream out;
    private FileOutputStream outPut;
    private ObjectInputStream in;
    private FileInputStream inPut;
    
    public StorageData(){}

    public StorageData(Serializable data){
        mSerializeData = data;
    }
    
    public StorageData(String phoneNumber, String mSid){
        mSerializeData = new SerializeData(phoneNumber, mSid);
    }

    public void writeIn(Context context, String fileName){
        try {
            File dir = context.getFilesDir();
            File key = new File(dir.getAbsolutePath()+"/" + fileName);
            if(!key.exists()){
                key.createNewFile();
            }
            outPut = context.openFileOutput(fileName, Context.MODE_PRIVATE);
            out = new ObjectOutputStream(outPut);
            out.writeObject(mSerializeData);
            out.close();
            outPut.close();
        } catch (IOException e) {
            Log.e(TAG, e.toString());
        }
    }

    public Serializable readOut(Context context, String fileName){
        File dir = context.getFilesDir();
        if(dir != null){
            File key = new File(dir.getAbsolutePath()+"/" + fileName);
            if(!key.exists()){
                return null;
            }
            try {
                inPut = context.openFileInput(fileName);
                in = new ObjectInputStream(inPut);
                Serializable mSerializeData = (Serializable)in.readObject();
                in.close();
                inPut.close();
                return mSerializeData;
            } catch (StreamCorruptedException e) {
                Log.e(TAG, "readOut StreamCorruptedException: " + e.toString());
            } catch (FileNotFoundException e) {
                Log.e(TAG, "readOut FileNotFoundException: " + e.toString());
            } catch (IOException e) {
                Log.e(TAG, "readOut IOException: " + e.toString());
            } catch (ClassNotFoundException e) {
                Log.e(TAG, "readOut ClassNotFoundException: " + e.toString());
            }
        }
        return null;
    }
    
    public void delete(Context context) {
        File dir = context.getFilesDir();
        File key = new File(dir.getAbsolutePath() + "/key.obj");
        if (key.exists()) {
            key.delete();
        }
        deleteDxCall(context);
        deleteSecondCall(context);
    }

    public void deleteDxCall(Context context){
        File dir = context.getFilesDir();
        File cheapCall = new File(dir.getAbsolutePath(),
                Constants.CHEAP_CALL_FILE_NAME);
        if (cheapCall.exists()) {
            cheapCall.delete();
        }
    }

    public void deleteSecondCall(Context context){
        File secondCall = new File(context.getFilesDir(), Constants.SECOND_CALL_FILE_NAME);
        if(secondCall.exists()){
            secondCall.delete();
        }
    }

    public String readScCall(Context c) {
        String result = null;
        SecondCallData reg = (SecondCallData)readOut(c, Constants.SECOND_CALL_FILE_NAME);
        if(reg != null){
            result = reg.getBind_number();
        }
        return result;
    }

    public SecondCallData readSecondCall(Context c){
        return (SecondCallData)readOut(c, Constants.SECOND_CALL_FILE_NAME);
    }

    public String readScBalance(Context c){
        String result = null;
        SecondCallData reg = (SecondCallData)readOut(c, Constants.SECOND_CALL_FILE_NAME);
        if(reg != null){
            result = reg.getBalance();
        }
        return result;
    }
    
    public void writeCheapCall(Context context, String phoneNum) {
        FileOutputStream fout = null;
        ObjectOutputStream oos = null;
        try {
            File dir = context.getFilesDir();
            File file = new File(dir.getAbsolutePath(), CHEAP_CALL_FILE);
            if (!file.exists()) {
                file.createNewFile();
            }
            
            fout = context.openFileOutput(CHEAP_CALL_FILE, Context.MODE_PRIVATE);
            oos = new ObjectOutputStream(fout);
            oos.writeUTF(phoneNum);
            oos.flush();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (fout != null) {
                try {
                    fout.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
